package cn.openea.perpartner.meily.web.controller;

import cn.openea.perpartner.common.core.utils.poi.ExcelUtil;
import cn.openea.perpartner.common.core.web.controller.BaseController;
import cn.openea.perpartner.common.core.web.domain.AjaxResult;
import cn.openea.perpartner.common.core.web.page.TableDataInfo;
import cn.openea.perpartner.meily.web.entity.dto.InvalidSysUserTemplateDTO;
import cn.openea.perpartner.meily.web.entity.dto.UserDetailDTO;
import cn.openea.perpartner.meily.web.entity.vo.SysUserSearchVo;
import cn.openea.perpartner.meily.web.service.ISysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.Collections;
import java.util.List;

/**
 * @author ysk
 * @version 1.0.1
 * @Description
 * @createTime 2023年04月12日 16:11:00
 */
@Api(tags = "用户信息")
@RestController
@RequestMapping("/sys/user")
public class SysUserController extends BaseController {

    @Autowired
    private ISysUserService sysUserService;

    @ApiOperation("分页查询-用户信息")
    @GetMapping("/page/list")
    public TableDataInfo list(SysUserSearchVo searchVo) {
        startPage();
        List<UserDetailDTO> userDetailDTOList = sysUserService.findList(searchVo);
        return getDataTable(userDetailDTOList);
    }

    @ApiOperation("全量导出")
    @PostMapping("/list/export")
    public void listAll(SysUserSearchVo searchVo, HttpServletResponse response) {
        List<UserDetailDTO> userDetailDTOList = sysUserService.findList(searchVo);
        // 导出
        ExcelUtil<UserDetailDTO> util = new ExcelUtil<>(UserDetailDTO.class);
        util.exportExcel(response, userDetailDTOList, "用户信息.xlsx");
    }

    @ApiOperation("分页查询-无效用户信息")
    @GetMapping("/page/invalid/list")
    public TableDataInfo invalidList(SysUserSearchVo searchVo) {
        startPage();
        List<UserDetailDTO> userDetailDTOList = sysUserService.findInvalidList(searchVo);
        return getDataTable(userDetailDTOList);
    }


    @ApiOperation("新增无效用户")
    @PostMapping("/invalid/add")
    public AjaxResult addInvalidUser(@RequestBody SysUserSearchVo vo) {
        if (StringUtils.isBlank(vo.getUserId())) {
            return AjaxResult.error("用户id不能为空");
        }
        sysUserService.addInvalidUser(vo.getUserId());
        return AjaxResult.success();
    }

    @ApiOperation("删除无效用户")
    @PostMapping("/invalid/delete")
    public AjaxResult deleteInvalidUser(@RequestBody List<String> userIds) {
        if (userIds.isEmpty()) {
            return AjaxResult.error("平台id不能为空");
        }
        sysUserService.deleteInvalidUser(userIds);
        return AjaxResult.success();
    }

    @ApiOperation("下载导入无效客户excel模板")
    @PostMapping("/download/import/invalid/template")
    public void downloadImportInvalidTemplate(HttpServletResponse response) {
        ExcelUtil<InvalidSysUserTemplateDTO> util = new ExcelUtil<>(InvalidSysUserTemplateDTO.class);
        util.exportExcel(response, Collections.singletonList(new InvalidSysUserTemplateDTO()), "导入无效用户数据模板.xlsx");
    }

    @ApiOperation("导入无效用户")
    @PostMapping("/import/invalid/list")
    public AjaxResult importInvalidUser(MultipartFile file) throws Exception {
        ExcelUtil<InvalidSysUserTemplateDTO> util = new ExcelUtil<>(InvalidSysUserTemplateDTO.class);
        List<InvalidSysUserTemplateDTO> invalidUserList = util.importExcel(file.getInputStream());
        sysUserService.saveBatchInvalidUser(invalidUserList);
        return AjaxResult.success("数据导入成功，已自动过滤错误和重复数据");
    }

}
